package service;

import mapper.VisitorRegisterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Visitor;
import util.MD5Utils;
import util.MailUtil;
import util.ServiceException;

import javax.mail.MessagingException;
import javax.mail.internet.ParseException;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
public class VisitorRegisterServiceImpl implements VisitorRegisterService {
    @Autowired
    private VisitorRegisterMapper visitorRegisterMapper;

    @Override
    public Visitor findVisitorByEmail(String email) {
        return visitorRegisterMapper.findVisitorByEmail(email);
    }

    @Override
    public void insertVisitor(Visitor visitor) {
         visitorRegisterMapper.insertVisitor(visitor);
    }

    @Override
    public void processregister(String email,String password,HttpServletRequest request) throws MessagingException {

        Visitor visitor=new Visitor();
        visitor.setEmail(email);
        visitor.setRegisterTime(new Date());
        visitor.setStatus(0);
        visitor.setPassword(password);
        visitor.setCode(MD5Utils.encode2hex(email));
        //根据email在数据库中查这个用户，如果有就更新，如果没有就插入数据库
        Visitor visitor2 = visitorRegisterMapper.findVisitorByEmail(email);
        if (visitor2 == null){
            //将注册的用户存进数据库
            visitorRegisterMapper.insertVisitor(visitor);
        }else {
            //数据库中已经有这个用户，说明这个用户注册了多次，但还没激活，就更新他的注册信息
            visitorRegisterMapper.update(visitor2);
        }

        ///邮件的内容
        StringBuffer sb=new StringBuffer("<!DOCTYPE html>\n" +
                                              "<html >\n" +
                                                 "<head>\n" +
                                                    "<meta charset=\"UTF-8\">\n" +
                                                 "</head>\n" +
                                                 "<body><p>点击下面链接激活账号，48小时生效，否则重新注册账号，链接只能使用一次，请尽快激活！</p>");
        sb.append("<a href=\"http://"+request.getServerName()+":"+request.getServerPort()+"/visitor/register.action?action=active&email=");
        sb.append(email);
        sb.append("&code=");
        sb.append(visitor.getCode());
        sb.append("\">点击激活");
        sb.append("</a>"+"</body></html>");
        //发送邮件
        MailUtil.send(email, sb.toString());
        System.out.println("发送邮件");
    }

    @Override
    public boolean processActivate(String email, String code) throws ServiceException, ParseException {

        Visitor visitor = visitorRegisterMapper.findVisitorByEmail(email);
        //验证用户是否存在
        if(visitor!=null) {
            //验证用户激活状态
            if(visitor.getStatus()==0) {
                ///没激活
                Date currentTime = new Date();//获取当前时间
                //验证链接是否过期
                //currentTime.before(visitor.getRegisterTime());
                if(currentTime.before(visitor.getLastActivateTime())) {
                    //验证激活码是否正确
                    if(code.equals(visitor.getCode())) {
                        //激活成功， //并更新用户的激活状态，为已激活
                        System.out.println("==sq==="+visitor.getStatus());
                        visitor.setStatus(1);//把状态改为激活
                        visitorRegisterMapper.update(visitor);
                        System.out.println("==sh==="+visitor.getStatus());
                        return  true;
                    } else {
                        throw new ServiceException("激活码不正确");
                    }
                } else { throw new ServiceException("激活码已过期！");
                }
            } else {
                throw new ServiceException("邮箱已激活，请登录！");
            }
        } else {
            throw new ServiceException("该邮箱未注册（邮箱地址不存在）！");
        }
    }

}
